<?php
class Activity extends Base
{

    private $_activityId;
    private $_title;
    private $_desciption;
    private $_address;
    private $_picture;
    private $_status;
    private $_voteId;


    protected function getPrimaryKey()
    {
        return "activityId";
    }

    protected function getTableName()
    {
        return "activity";
    }

    public function findLast($count)
    {
        $sql = "select * from activity order by createDate desc limit $count";
        return $this->_db->getall($sql);
    }

    public function getTotalCount()
    {
        $sql = "select count(*) as count from " . $this->getTableName() . " where status <> 'EXPIRED'";
        $count = $this->_db->getOne($sql);
        return $count['count'];
    }

    public function getVote()
    {
        include_once("Vote.class.php");
        $vote = new Vote();
        return $vote->loadAsObject($this->getVoteId());
    }

    public function setActivityId($activityId)
    {
        $this->_activityId = $activityId;
    }

    public function getActivityId()
    {
        return $this->_activityId;
    }

    public function setAddress($address)
    {
        $this->_address = $address;
    }

    public function getAddress()
    {
        return $this->_address;
    }

    public function setDesciption($desciption)
    {
        $this->_desciption = $desciption;
    }

    public function getDesciption()
    {
        return $this->_desciption;
    }

    public function setPicture($picture)
    {
        $this->_picture = $picture;
    }

    public function getPicture()
    {
        return $this->_picture;
    }

    public function setStatus($status)
    {
        $this->_status = $status;
    }

    public function getStatus()
    {
        return $this->_status;
    }

    public function setTitle($title)
    {
        $this->_title = $title;
    }

    public function getTitle()
    {
        return $this->_title;
    }

    public function setVoteId($voteId)
    {
        $this->_voteId = $voteId;
    }

    public function getVoteId()
    {
        return $this->_voteId;
    }

    public function findNearliest()
    {
        $sql = "select * from activity where phases='NOW' order by amount asc limit 1";
        return $this->_db->getOne($sql);
    }

    public function findNearlier($count)
    {
        $sql = "select * from activity where phases='LATER' order by startDate asc limit $count";
        return $this->_db->getAll($sql);
    }

    public function isIpVoted($ip)
    {

    }

    public function doVote($activityId, $userId, $ip)
    {
        // $options = array("hao", "zhong", "cha");
        //  if (in_array($voteOption, $options)) {
        $sql = "select count(1) as count from log where operator_id='" . $userId . "' and operated_id='" . $activityId . "' and type='ACTIVITY_VOTE'";
        $count = $this->_db->getOne($sql);
        if ($count['count'] == 0) {
            $sql = "insert into log (createDate,type,ip,operator_id,operated_id) values ('" . Util::getCurrentDate() . "','ACTIVITY_VOTE','" . $ip . "','" . $userId . "','" . $activityId . "')";
            $this->_db->query($sql);
            // $sql = "update activity set " . $voteOption . "=" . $voteOption . "+1 where activityId = $activityId";
            $sql2 = "update activity set amount = amount+1 where activityId = $activityId";
            $this->_db->query($sql);
            $this->_db->query($sql2);
            return "投票成功，谢谢您的参与";
        } else {
            return "对不起，您已经参与了投票";
        }
        // }
        //  return "非法操作";


    }

    protected function competeRecord($post)
    {
        $record = array(
            "title" => $post['title'],
            "description" => $post['description'],
            "address" => $post['address'],
            "startDate" => $post['startDate'],
            "endDate" => $post['endDate'],
            "picture" => $post['picture'],
            "tel" => $post['tel'],
            "createDate" => date("Y-m-d H:i:s"),
            "status" => 1,
            "phases" => $post['phases']
        );
        return $record;
    }

    public function findRelatedCommentByUserId($userId)
    {
        $sql = "select a.* from activity a,log l where a.activityId = l.operated_id and l.type='ACTIVITY_VOTE' and l.operator_id=$userId";
        return $this->_db->getall($sql);
    }


}

?>